package com.sugon.modules.device.service.impl;

import cn.hutool.core.date.DateUtil;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.entity.ExcepRecordEntity;
import com.sugon.entity.InspectionRecordingJoinEntity;
import com.sugon.modules.device.consts.DeviceInspectionResult;
import com.sugon.modules.device.consts.ExceptionSourceType;
import com.sugon.modules.device.model.vo.DevInspectExceptionReportParam;
import com.sugon.modules.device.service.IDeviceInspectionManager;
import com.sugon.service.DeviceInfoService;
import com.sugon.service.ExcepRecordService;
import com.sugon.service.InspectionRecordingJoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ： YuXD
 * @description： 设备巡检业务Manager实现类
 * @date ： 2020-08-12 下午 9:30
 * @version: 1.0
 */
@Service
public class DeviceInspectionManagerImpl implements IDeviceInspectionManager {

    @Autowired
    private InspectionRecordingJoinService inspectionRecordingJoinService;
    @Autowired
    private ExcepRecordService excepRecordService;
    @Autowired
    private DeviceInfoService deviceInfoService;

    /**
     * addNormalDevInspectionRecord
     * @param inspectionRecord 设备巡检记录
     * @return
     */
    @Override
    public boolean addNormalDevInspectionRecord(InspectionRecordingJoinEntity inspectionRecord) {
        inspectionRecord.setIsNormal(DeviceInspectionResult.NORMAL);
        inspectionRecord.setInspectDate(DateUtil.date());
        return inspectionRecordingJoinService.save(inspectionRecord) > 0;
    }

    /**
     * addExceptionDevInspectionRecord
     * @param devInspectExceptionReportParam 设备巡检异常上报参数
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addExceptionDevInspectionRecord(DevInspectExceptionReportParam devInspectExceptionReportParam) {
        // 获取设备编码
        String devId = devInspectExceptionReportParam.getDevId();
        // 生成设备巡检记录和异常记录
        InspectionRecordingJoinEntity devInspectionRecord = new InspectionRecordingJoinEntity();
        devInspectionRecord.setDevId(devId);
        devInspectionRecord.setInspectionOptionId(devInspectExceptionReportParam.getInspectionOptionId());
        devInspectionRecord.setIsNormal(DeviceInspectionResult.EXCEPTION);
        devInspectionRecord.setPlanPerson(devInspectExceptionReportParam.getPlanPersonId());
        devInspectionRecord.setInspectDate(DateUtil.date());
        if (inspectionRecordingJoinService.save(devInspectionRecord) > 0) {
            ExcepRecordEntity excepRecord = new ExcepRecordEntity();
            excepRecord.setExType(ExceptionSourceType.INSPECTION);
            excepRecord.setExcepCon(devInspectExceptionReportParam.getExcepCon());
            excepRecord.setExcepDescr(devInspectExceptionReportParam.getExcepDescr());
            excepRecord.setAdvice(devInspectExceptionReportParam.getAdvice());
            excepRecord.setOriginRecordId(devInspectionRecord.getId());
            excepRecord.setDutyUserId(devInspectionRecord.getPlanPerson());
            excepRecord.setCreateUser(devInspectionRecord.getPlanPerson());
            DeviceInfoEntity deviceInfo = deviceInfoService.queryObject(devId);
            if (deviceInfo != null) {
                excepRecord.setDevId(deviceInfo.getId());
                excepRecord.setWorkshopId(deviceInfo.getDevFarm());
            }
            excepRecordService.save(excepRecord);
        }
        return true;
    }
}
